﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="../../Libs/knockout-2.0.0.debug.js" type="text/javascript"></script>
    <script src="../../Libs/json2.js" type="text/javascript"></script>
    <script src="../../Release/entityspaces.XHR.debug.js" type="text/javascript"></script>
    <script src="Generated/Employees.js" type="text/javascript"></script>
    <script src="Custom/Employees.js" type="text/javascript"></script>
    <script src="Generated/Orders.js" type="text/javascript"></script>
    <script src="Generated/OrderDetails.js" type="text/javascript"></script>
    <title>EntitySpaces.js Basic Example</title>
</head>
<body>
    <button onclick="test1();">Test 1</button><br /><br />
    <button onclick="test2();">Test 2</button><br /><br />
    <button onclick="test3();">Test 3</button><br /><br />
    <button onclick="test4();">Test 4</button><br /><br />
</body>
</html>

<script language="javascript" type="text/javascript">

 // es.dataProvider.baseURL = "http://localhost/esService/esJson.svc/";
    es.dataProvider.baseURL = "http://www.entityspaces.net/Knockout/Part1/esService/esJson.svc/";

    test1 = function () {

        // Load an employee with hierarchical model and save hierarchical data back to the server
        var emp = new es.objects.Employees();
        if(emp.LoadByPrimaryKeyHierarchical(2)) {  // <== this method is in the Custom "Employee.js" file
            emp.FirstName("Thiss");
            emp.OrdersCollectionByEmployeeID()[0].ShipCity(Math.random().toString().substr(0, 4));
            emp.save();
        }

        // Collection load/save
        var coll = new es.objects.EmployeesCollection();
        coll.loadAll();

        coll()[0].FirstName("Rocks!!");
        coll.save();
    }

    test2 = function () {

        //==========================================================================
        // The XMLHttpRequestProvider allows you to make any call you desire
        //==========================================================================
        var request = es.dataProvider.makeRequest(es.dataProvider.baseURL, "Employees_LoadHierarchical", null);

        es.dataProvider.makeRequest(es.dataProvider.baseURL, "Employees_LoadHierarchical", null,
        function (data) {
            // Success ...
        },
        function (status, error) {
            // Fail ...
            var f = 0;
        });

        //----------------------------------------------------------
        // Here is a code snippet using the synchronous approach
        //----------------------------------------------------------
        var emp = new es.objects.Employees();
        if (emp.loadByPrimaryKey(2)) {

            emp.FirstName("sync" + "!!!");
            emp.save();
        }

        var coll = new es.objects.EmployeesCollection();
        coll.loadAll();

        coll()[0].FirstName("Rocks!!");
        coll.save();

        //-----------------------------------------------------------------
        // Here is the same code from above using the asynchronous approach
        //-----------------------------------------------------------------
        var emp = new es.objects.Employees();
        emp.loadByPrimaryKey(2, function (data, state) {

            if (state.wasLoaded) {

                emp.FirstName("sync" + "!!!");

                emp.save(function (data) {

                    var coll = new es.objects.EmployeesCollection();

                    coll.loadAll(function (data) {

                        coll()[0].FirstName("Rocks!!");

                        coll.save(function (data) {

                            var str = "Save is complete ...";
                        });
                    });
                });
            }
        }, null, "MyStateData");
    }

    test3 = function () {

        // Add a single record
        var emp = new es.objects.Employees();
        emp.FirstName("Just");
        emp.LastName("Added");
        emp.save();

        // It's an autoincrement column and we get it back
        var employeeId = emp.EmployeeID();

        // Add two new employees through a collection
        var coll = new es.objects.EmployeesCollection();

        emp = new es.objects.Employees();
        emp.FirstName("Just1");
        emp.LastName("Added1");
        coll.push(emp);

        emp = new es.objects.Employees();
        emp.FirstName("Just2");
        emp.LastName("Added2");
        coll.push(emp);

        coll.save();

        // Check to make sure we got our autoincrement primary keys
        var employeeId_1 = coll()[0].EmployeeID();
        var employeeId_2 = coll()[1].EmployeeID();
    }

    test4 = function () {

        // Add a single record
        var emp = new es.objects.Employees();
        emp.FirstName("Just");
        emp.LastName("Added");
        emp.save();

        var employeeId = emp.EmployeeID();

        // Reload the new record and delete it
        emp = new es.objects.Employees();
        if(emp.loadByPrimaryKey(employeeId)) {
            emp.markAsDeleted();
            emp.save();
        }

        // Can we reload the deleted record
        emp = new es.objects.Employees();
        var loaded = emp.loadByPrimaryKey(employeeId);

        // The employeeId = undefined
        employeeId = emp.EmployeeID();
    }

</script>